package com.study.mybatis;

import java.sql.*;

public class Test1 {

    public static void main1(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/zhen?useUnicode=true&characterEncoding=UTF8"
                , "root", "root");
        PreparedStatement statement  = connection.prepareStatement("select id, username from t_user where username = ? and password = ?");
        statement.setString(1, "zhangsan");
        statement.setString(2, "1");
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            Integer userId = resultSet.getInt(1);
            String userName = resultSet.getString(2);
            System.out.println("有用户userId:" + userId + ", userName:" + userName);
        } else {
            System.out.println("无此用户");
        }
        statement.close();
        connection.close();
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/zhen?useUnicode=true&characterEncoding=UTF8"
                , "root", "root");
        PreparedStatement statement  = connection.prepareStatement("select count(1) from t_user where username = ? and password = ?");
        statement.setString(1, "zhangsan");
        statement.setString(2, "2");
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            int userCount = resultSet.getInt(1);
            if (userCount == 0) {
                System.out.println("无此用户");
            } else {
                System.out.println("有次用户"+ userCount);
            }
        } else {
            System.out.println("无此用户");
        }
        statement.close();
        connection.close();
    }
}
